package com.aic.aicdetactor;

import android.Manifest;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;

import com.aic.aicdetactor.service.AICService;
import com.aic.aicdetactor.util.ToastUtil;
import com.aicmonitor.aicdetactorsimple.R;

public class MainActivityV2 extends Activity {

    private int REQUEST_ENABLE_BT = 11;

    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_FINE_LOCATION};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainv2);
        startService(new Intent(MainActivityV2.this,AICService.class));
        //检查权限
        checkBluetoothPermission();
    }

    /*
       校验蓝牙权限
       */
    private void checkBluetoothPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            //校验是否已具有模糊定位权限
            if (ContextCompat.checkSelfPermission(MainActivityV2.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    || ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                    || ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                    || ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(MainActivityV2.this, PERMISSIONS_STORAGE, REQUEST_ENABLE_BT);
            } else {//权限已打开
                ToastUtil.showToast(this, "蓝牙权限已打开");
                doFinish();
            }
        } else {//小于23版本直接使用
            ToastUtil.showToast(this, "API小于23版本直接使用");
            doFinish();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_ENABLE_BT) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {//蓝牙权限开启成功
                ToastUtil.showToast(this, "蓝牙权限已打开");
                doFinish();
            } else {
                ToastUtil.showToast(MainActivityV2.this, "蓝牙权限未开启,请手动设置,否则无法正常使用");
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    private Handler handler;
    private Runnable runnable;
    private BluetoothAdapter bluetoothAdapter;
    private void doFinish(){
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        bluetoothAdapter.enable();
        if (bluetoothAdapter.isEnabled()){
            bluetoothAdapter.enable();
            handler = new Handler();
            runnable = new Runnable() {
                @Override
                public void run() {
                    if (BluetoothAdapter.STATE_ON == bluetoothAdapter.getState()){
                        handler.removeCallbacks(runnable);
                        finish();
                    }else{
                        handler.postDelayed(runnable,200);
                    }
                }
            };
            handler.postDelayed(runnable,200);
        }else{
            finish();
        }

    }
}
